home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / isapi / ISAPIResponse.java < prev    next >
Encoding:
Java Source  |  1997-07-18  |  3.5 KB  |  137 lines

  1. /*
  2.  * @(#)ISAPIResponse.java    1.11 97/07/16
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. package sun.servlet.isapi;
  23.  
  24. import javax.servlet.ServletOutputStream;
  25. import sun.servlet.http.HttpResponse;
  26. import java.io.IOException;
  27.  
  28. /**
  29.  * This class represents a servlet response in the ISAPI extension version
  30.  * of the servlet runner.
  31.  *
  32.  * @version    1.11, 07/16/97
  33.  * @author    David Connelly
  34.  * @author    Jongyoon Lee
  35.  */
  36. class ISAPIResponse extends HttpResponse {
  37.     /*
  38.      * The ISAPIConnection for this response
  39.      */
  40.     private ISAPIConnection conn;
  41.  
  42.     ISAPIResponse() {
  43.     this(null);
  44.     }
  45.  
  46.     ISAPIResponse(ISAPIConnection conn) {
  47.     super();
  48.     out = new ISAPIOutputStream();
  49.     init(conn);
  50.     }
  51.  
  52.     public void init(ISAPIConnection conn) {
  53.     this.conn = conn;
  54.     ((ISAPIOutputStream)out).init(conn);
  55.     out.setObserver(this);
  56.     keepAlive = false;
  57.     }
  58.  
  59.     /**
  60.      * Returns an output stream for writing response data.
  61.      */
  62.     public ServletOutputStream getOutputStream() {
  63.     return out;
  64.     }
  65.  
  66.     /**
  67.      * Sends an error response to the client using the specified status
  68.      * code and detail message.
  69.      * @param sc the status code
  70.      * @param msg the detail message
  71.      * @exception IOException If an I/O error has occurred.
  72.      */
  73.     public void sendError(int sc, String msg) throws IOException {
  74.     ISAPIOutputStream out = (ISAPIOutputStream)this.out;
  75.     if (!out.isCommitted()) {
  76.         headers.clear();
  77.         out.next();
  78.         out.setObserver(this);
  79.         out.setIOException(null);
  80.         setStatus(sc);
  81.         setContentType("text/html");
  82.         String title = status + " " + reason;
  83.         out.print("<head><title>");
  84.         out.print("Servlet Runner");
  85.         out.println("</title></head>");
  86.         out.print("<h1>");
  87.         out.print(title);
  88.         out.println("</h1><body>");
  89.         if (msg != null) {
  90.         out.print(msg);
  91.         out.println("<p>");
  92.         }
  93.         out.println("</body>");
  94.         out.finish();
  95.     }
  96.     }
  97.  
  98.     /*
  99.      * Writes status line and headers to specified servlet output stream.
  100.      */
  101.     protected void writeHeaders() throws IOException {
  102.     bufHeader.reset();
  103.     outHeader.next();
  104.     outHeader.init(bufHeader);
  105.     int len;
  106.     if ((len = out.getContentLength()) != -1) {
  107.         if (!contentLenSet) {
  108.         headers.putIntHeader("Content-Length", len);
  109.         }
  110.     }
  111.     headers.write(outHeader);
  112.     outHeader.flush();
  113.     if (status == 0) {
  114.         status = SC_OK;
  115.         reason = reason(SC_OK);
  116.     }
  117.     byte[] buf = bufHeader.toByteArray();
  118.     headerBytes = buf.length;
  119.     String stat = new String(status + " " + reason);
  120.     byte[] statbuf = stat.getBytes();
  121.     conn.writeHeaders(statbuf, 0, statbuf.length, buf, 0, buf.length);
  122.     }
  123.  
  124.     /**
  125.      * Finishes the response.
  126.      */
  127.     public void finish() throws IOException {
  128.     if (out != null) {
  129.         out.finish();
  130.     }
  131.     if (conn != null) {
  132.         conn.finish();
  133.         conn = null;
  134.     }
  135.     }
  136. }
  137.